package io.techery.progresshint.addition;

import io.techery.progresshint.ProgressHintDelegate;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Slider;
import ohos.agp.utils.Point;

import ohos.multimodalinput.event.TouchEvent;

public class HorizontalProgressHintDelegate extends ProgressHintDelegate {
    /**
     * HorizontalProgressHintDelegate Instance
     *
     * @param seekBar seekBar
     * @param attrSet attrSet
     * @param styleName styleName
     */
    public HorizontalProgressHintDelegate(Slider seekBar, AttrSet attrSet, String styleName) {
        super(seekBar, attrSet, styleName);
    }

    @Override
    protected Point getHintDragCoordinates(TouchEvent event) {
        float floatX = event.getPointerScreenPosition(event.getIndex()).getX() - mSeekBar.getContentPositionX();
        float floatY = mSeekBar.getContentPositionY();
        return new Point(floatX, floatY);
    }

    @Override
    protected Point getFixedHintOffset() {
        int xOffset = getHorizontalOffset(mSeekBar.getMax() / 2);
        int yOffset = getVerticalOffset();
        return new Point(xOffset, yOffset);
    }

    @Override
    protected Point getFollowHintOffset() {
        int xOffset = getHorizontalOffset(mSeekBar.getProgress());
        int yOffset = getVerticalOffset();
        return new Point(xOffset, yOffset);
    }

    private int getHorizontalOffset(int progress) {
        return getFollowPosition(progress) - mPopupView.getEstimatedWidth() / 2
                + mSeekBar.getHeight() / 2 + 15;
    }

    private int getVerticalOffset() {
        return -(mSeekBar.getHeight() + mPopupView.getEstimatedHeight() + mPopupOffset);
    }

    @Override
    public boolean isWidgetFullyVisible(Component container) {
        int relativeTop = ViewUtil.getRelativeTop(mSeekBar, container);
        return (container.getEstimatedHeight() < relativeTop - mPopupView.getHeight() - mPopupOffset)
                && (container.getHeight() + container.getEstimatedHeight() - mPopupView.getHeight() - mPopupOffset >
                relativeTop - mSeekBar.getHeight());
    }
}
